package com.cjjd.c.feature.home.call;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HotelQrCodeInfoBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.dialog.TextDialog;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.DialogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/22.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class CallFragment extends ActionBarFragment {

    @Override
    public Object getTitleString() {
        return "呼叫客服";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_call;
    }


    private String phonenum;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getHotelMessage();
    }

    private  HotelQrCodeInfoBean bean;
    private  boolean isOk;
    private void getHotelMessage(){
        DialogUtil.delDialog(mContext);
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("hotelUuid",SharePrefUtil.getString(mContext,ShareKeys.HomeHotelUuid));
        service.getHotelQrCodeInfo(JsonUtils.convertParam("getHotelQrCodeInfo", h1)).enqueue(new CallbackImpl<HotelQrCodeInfoBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<HotelQrCodeInfoBean>> call, Response<HttpResult<HotelQrCodeInfoBean>> response) {
                bean=response.body().data;
                if(isOk) {
                    if (TextUtils.isEmpty(bean.customerService)) {
                        new TextDialog(mContext, "暂无投诉建议任县", new TextDialog.OnCloseListener() {
                            @Override
                            public void OnClick() {

                            }
                        }).show();
                    }
                }
                DialogUtil.dismissLoadingDialog(mContext);
            }

            @Override
            public void onFailure(Call<HttpResult<HotelQrCodeInfoBean>> call, Throwable t) {
                super.onFailure(call, t);
                DialogUtil.dismissLoadingDialog(mContext);
            }

        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.zhuanjia, R.id.rexian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zhuanjia:
                ToastUtil.showToast("功能暂未开通");
                break;
            case R.id.rexian:
                if(bean!=null){
                    onViewClicked();
                }else{
                    isOk=true;
                    getHotelMessage();
                }
                break;
        }
    }




    public void onViewClicked() {



        if (Build.VERSION.SDK_INT >= 23) {

            //判断有没有拨打电话权限
            if (PermissionChecker.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                //请求拨打电话权限
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);

            } else {
                call();
            }

        } else {
            call();
        }
    }
    private final int REQUEST_CODE = 0x1001;
    /**
     * 请求权限的回调方法
     * @param requestCode    请求码
     * @param permissions    请求的权限
     * @param grantResults   权限的结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE && PermissionChecker.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            call();
        }
    }
    private void call(){

        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+bean.customerService));
            mContext.startActivity(intent);
        }catch (SecurityException e){
        }

    }
}
